package com.yisu.expressway.customService.adapter;

import android.databinding.DataBindingUtil;
import android.graphics.drawable.Drawable;
import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.yisu.expressway.R;
import com.yisu.expressway.customService.TTSPlayer;
import com.yisu.expressway.customService.VoiceAssistantActivity;
import com.yisu.expressway.customService.model.ChatContentObj;
import com.yisu.expressway.customService.model.HighWayInfoObj;
import com.yisu.expressway.databinding.ChatAnswerBinding;
import com.yisu.expressway.databinding.ChatQuestionBinding;
import com.yisu.expressway.databinding.TextWithActionItemBinding;
import com.yisu.expressway.databinding.TextWithArrowForDatabindingBinding;
import com.yisu.expressway.databinding.VoiceAnswerHeaderBinding;
import com.yisu.expressway.databinding.VoiceDefaultAnswerBinding;
import com.yisu.expressway.listener.onClickHandler;
import com.yisu.expressway.service_district.DistrictDetailActivity;
import com.yisu.expressway.settings.AppConstant;
import com.yisu.expressway.ui.recyclerview.DisableScrollLinearLayoutManager;
import com.yisu.expressway.utils.PhoneCallUtils;
import com.yisu.expressway.utils.ToastUtil;
import com.yisu.expressway.weather.ResUtils;
import com.yisu.expressway.weather.WeatherActivity;

import java.util.ArrayList;

/**
 * Created by theodore_hu on 2016/12/21 0021.
 */

public class VoiceAssistantAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {


    public final static int ITEM_TYPE_QUESTION = 1;
    public final static int ITEM_TYPE_UNKNOWN_ANSWER = 2;

    public final static int QUESTION_TYPE_IFLY = 0;
    public final static int QUESTION_TYPE_CONDITION = 11;
    public final static int QUESTION_TYPE_NAV = 12;
    public final static int QUESTION_TYPE_FEE_RULES = 13;
    public final static int QUESTION_TYPE_WEATHER = 14;
    public final static int QUESTION_TYPE_DISTRICT = 15;
    public final static int QUESTION_TYPE_ILLEGAL = 16;
    public final static int QUESTION_TYPE_FEE_STANDARD = 17;
    public final static int QUESTION_TYPE_RESCUE= 18;
    public final static int QUESTOIN_TYPE_SERRCHE_DISTRICT = 19;

    @NonNull
    private ArrayList<ChatContentObj> mData = new ArrayList<>();
    VoiceAssistantActivity mActivity;

    public VoiceAssistantAdapter(VoiceAssistantActivity activity){
        mActivity = activity;
    }

    public void updateData(ChatContentObj chatContentObj){
        mData.add(chatContentObj);

        if(mData.size() >= 2) {
            notifyItemRangeChanged(mData.size() - 2, mData.size() - 1);
        }else {
            notifyItemInserted(mData.size() - 1);
        }
//        notifyDataSetChanged();
        if(!chatContentObj.isQuestion() && !chatContentObj.isHeader()){
            TTSPlayer.getmInstance().playText(chatContentObj.getText());
        }
    }

    @Override
    public int getItemViewType(int position) {
        return mData.get(position).getQuestionType();
    }


    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView;
        switch (viewType){
            case ITEM_TYPE_QUESTION:
                itemView = LayoutInflater.from(parent.getContext())
                        .inflate(R.layout.chat_question, parent, false);
                return new QuestionViewHolder(itemView);
            case QUESTION_TYPE_IFLY:
                itemView = LayoutInflater.from(parent.getContext())
                        .inflate(R.layout.chat_answer, parent, false);
                return new AnswerViewHolder(itemView);
            case QUESTION_TYPE_WEATHER:
                itemView = LayoutInflater.from(parent.getContext())
                        .inflate(R.layout.text_with_action_item, parent, false);
                return new WeatherViewHolder(itemView);
            case QUESTION_TYPE_RESCUE:
                itemView = LayoutInflater.from(parent.getContext())
                        .inflate(R.layout.voice_answer_header, parent, false);
                return new HighWayRescueHolder(itemView);
            case QUESTION_TYPE_DISTRICT:
                itemView = LayoutInflater.from(parent.getContext())
                        .inflate(R.layout.voice_answer_header, parent, false);
                return new DistrictViewHolder(itemView);
            case QUESTOIN_TYPE_SERRCHE_DISTRICT:
                itemView = LayoutInflater.from(parent.getContext())
                        .inflate(R.layout.text_with_action_item, parent, false);
                return new SingleDistrictViewHolder(itemView);
            case ITEM_TYPE_UNKNOWN_ANSWER:
                itemView = LayoutInflater.from(parent.getContext())
                        .inflate(R.layout.voice_default_answer, parent, false);
                return new UnknownViewHolder(itemView);

        }


        return null;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if(holder instanceof AnswerViewHolder) {
            ((AnswerViewHolder)holder).bind(mData.get(position));
        }else if(holder instanceof QuestionViewHolder){
            ((QuestionViewHolder)holder).bind(mData.get(position));
        }else if(holder instanceof WeatherViewHolder){
            ((WeatherViewHolder)holder).bind(mData.get(position));
        }else if(holder instanceof HighWayRescueHolder){
            ((HighWayRescueHolder)holder).bind(mData.get(position));
        }else if(holder instanceof DistrictViewHolder){
            ((DistrictViewHolder)holder).bind(mData.get(position));
        }else if(holder instanceof SingleDistrictViewHolder){
            ((SingleDistrictViewHolder)holder).bind(mData.get(position));
        }else if(holder instanceof UnknownViewHolder){
            ((UnknownViewHolder)holder).bind(mData.get(position));
        }
    }

    @Override
    public int getItemCount() {
        return mData == null ? 0 : mData.size();
    }


    public  class AnswerViewHolder extends RecyclerView.ViewHolder implements onClickHandler  {
        private ChatAnswerBinding itemBinding;

        public AnswerViewHolder(View view){
            super(view);
            itemBinding = DataBindingUtil.bind(view);
        }

        public void bind(@NonNull ChatContentObj answerObj){
            itemBinding.setChatAnswer(answerObj);
            itemBinding.setClickListener(this);
        }

        @Override
        public void handleClick(View view) {
            ChatContentObj selectItem = itemBinding.getChatAnswer();
            if(selectItem != null){
                 TTSPlayer.getmInstance().playText(selectItem.getText());
            }
        }
    }

    public  class QuestionViewHolder extends RecyclerView.ViewHolder{
        private ChatQuestionBinding itemBinding;

        public QuestionViewHolder(View view){
            super(view);
            itemBinding = DataBindingUtil.bind(view);
        }

        public void bind(@NonNull ChatContentObj questionContent){
            itemBinding.setChatContent(questionContent);
        }
    }

    public class WeatherViewHolder extends RecyclerView.ViewHolder implements onClickHandler{
        private TextWithActionItemBinding itemBinding;
        public WeatherViewHolder(View view){
            super(view);
            itemBinding =  DataBindingUtil.bind(view);
        }

        public void bind(@NonNull ChatContentObj questionContent){
            itemBinding.setAnswerItem(questionContent);
            itemBinding.setClickListner(this);
            Drawable drawable= mActivity.getResources().getDrawable(ResUtils.getWeatherIcon(questionContent.getWeatherCode()));
            drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
            itemBinding.tvSecondaryInfo.setCompoundDrawables(drawable,null,null,null);
        }

        @Override
        public void handleClick(View view) {
            ChatContentObj selectItem = itemBinding.getAnswerItem();
            WeatherActivity.launch(mActivity,selectItem.getWeatherObj(),selectItem.getWeatherCity());
        }
    }

    public class HighWayRescueHolder extends RecyclerView.ViewHolder{

        private VoiceAnswerHeaderBinding itemBinding;
        private VoiceListItemAdapter itemAdapter;

        public HighWayRescueHolder(View view){
            super(view);
            itemBinding =  DataBindingUtil.bind(view);
            itemBinding.contentList.setLayoutManager(new LinearLayoutManager(mActivity));
            itemAdapter = new VoiceListItemAdapter(mActivity);
            itemBinding.contentList.setAdapter(itemAdapter);
        }

        public void bind(@NonNull ChatContentObj questionContent) {
            itemBinding.setHeadTitle(questionContent);
            itemAdapter.update(questionContent.getHighWayInfoList());
        }
    }

    public class DistrictViewHolder extends RecyclerView.ViewHolder{

        private VoiceAnswerHeaderBinding itemBinding;
        private DistrictListItemAdapter itemAdapter;

        public DistrictViewHolder(View view){
            super(view);
            itemBinding =  DataBindingUtil.bind(view);
            itemBinding.contentList.setLayoutManager(new DisableScrollLinearLayoutManager(mActivity));
            itemAdapter = new DistrictListItemAdapter(mActivity);
            itemBinding.contentList.setAdapter(itemAdapter);
        }

        public void bind(@NonNull ChatContentObj questionContent) {
            itemBinding.setHeadTitle(questionContent);
            itemAdapter.update(questionContent.getDistrictItemList());
        }
    }

    public class SingleDistrictViewHolder extends RecyclerView.ViewHolder implements onClickHandler {
        private TextWithActionItemBinding itemBinding;

        public SingleDistrictViewHolder(View view) {
            super(view);
            itemBinding = DataBindingUtil.bind(view);
        }

        public void bind(@NonNull ChatContentObj questionContent) {
            itemBinding.setAnswerItem(questionContent);
            itemBinding.setClickListner(this);
        }

        @Override
        public void handleClick(View view) {
            ChatContentObj selectItem = itemBinding.getAnswerItem();
            DistrictDetailActivity.launch(mActivity, selectItem.getServiceDistrictItem(),false);
        }
    }

    public  class UnknownViewHolder extends RecyclerView.ViewHolder implements onClickHandler  {
        private VoiceDefaultAnswerBinding itemBinding;

        public UnknownViewHolder(View view){
            super(view);
            itemBinding = DataBindingUtil.bind(view);
        }

        public void bind(@NonNull ChatContentObj answerObj){
            itemBinding.setAnswerItem(answerObj);
            itemBinding.setOnLayoutClick(this);
            itemBinding.setOnNumberClick(this);
        }

        @Override
        public void handleClick(View view) {
            switch (view.getId()){
                case R.id.tv_custome_number:
                    PhoneCallUtils.makePhoneCall(mActivity, AppConstant.CUSTOMER_SERVICE_PHONE_NUMBER);
                    break;
                case R.id.ll_answer_root:
                    TTSPlayer.getmInstance().playText(mActivity.getString(R.string.default_answer_read_text));
                    break;
                default:
                    break;
            }


        }
    }

}
